function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}

var imageCodeId = "";

function generateUUID() {
    var d = new Date().getTime();
    if (window.performance && typeof window.performance.now === "function") {
        d += performance.now(); //use high-precision timer if available
    }
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
}

function generateImageCode() {
    imageCodeId = generateUUID();
    var url = '/api/v1.0/verify_code?code_id=' + imageCodeId;
    $('.image-code img').attr('src', url);
}


function sendSMSCode() {
    var reMobile = /^1[3578]\d{9}$/;
    $(".phonecode-a").removeAttr("onclick");
    var mobile = $("#mobile").val();
    // alert(re)
    if (!(reMobile.test(mobile))) {
        // alert(mobile);
        $("#mobile-err span").html("请填写正确的手机号！");
        $("#mobile-err").show();
        $(".phonecode-a").attr("onclick", "sendSMSCode();");
        return;
    }

    if (!mobile) {
        $("#mobile-err span").html("请填写正确的手机号！");
        $("#mobile-err").show();
        $(".phonecode-a").attr("onclick", "sendSMSCode();");
        return;
    }
    var imageCode = $("#imagecode").val();
    if (!imageCode) {
        $("#image-code-err span").html("请填写验证码！");
        $("#image-code-err").show();
        $(".phonecode-a").attr("onclick", "sendSMSCode();");
        return;
    }

    // 验证通过就让他停止60s
    var $time = $(".phonecode-a");
    var duration = 60;
    var intervalid = setInterval(function () {
        $time.html(duration + "秒");
        if (duration === 1) {
            clearInterval(intervalid);
            $time.html('获取验证码');
            $(".phonecode-a").attr("onclick", "sendSMSCode();");
        }
        duration = duration - 1;
    }, 1000, 60);
    var url = "/api/v1.0/sms_code/" + mobile;
    $.get(url, {code_text: imageCode, code_id: imageCodeId},
        function (data) {
            if (0 != data.errno) {
                // alert('发送短信失败')
            }

            // if (0 != data.errno) {
            //     $("#image-code-err span").html(data.errmsg);
            //     $("#image-code-err").show();
            //     if (2 == data.errno || 3 == data.errno) {
            //         generateImageCode();
            //     }
            //     $(".phonecode-a").attr("onclick", "sendSMSCode();");
            // }
            // else {
            //     var $time = $(".phonecode-a");
            //     var duration = 60;
            //     var intervalid = setInterval(function(){
            //         $time.html(duration + "秒");
            //         if(duration === 1){
            //             clearInterval(intervalid);
            //             $time.html('获取验证码');
            //             $(".phonecode-a").attr("onclick", "sendSMSCode();");
            //         }
            //         duration = duration - 1;
            //     }, 1000, 60);

        }, 'json');
}

$(document).ready(function () {
    generateImageCode();
    $("#mobile").focus(function () {
        $("#mobile-err").hide();
    });
    $("#imagecode").focus(function () {
        $("#image-code-err").hide();
    });
    $("#phonecode").focus(function () {
        $("#phone-code-err").hide();
    });
    $("#password").focus(function () {
        $("#password-err").hide();
        $("#password2-err").hide();
    });
    $("#password2").focus(function () {
        $("#password2-err").hide();
    });
    $(".form-register").submit(function (e) {

        e.preventDefault();

        var mobile = $("#mobile").val();
        var phoneCode = $("#phonecode").val();
        var passwd = $("#password").val();
        var passwd2 = $("#password2").val();
        if (!mobile) {
            $("#mobile-err span").html("请填写正确的手机号！");
            $("#mobile-err").show();
            return;
        }
        if (!phoneCode) {
            $("#phone-code-err span").html("请填写短信验证码！");
            $("#phone-code-err").show();
            return;
        }
        if (!passwd) {
            $("#password-err span").html("请填写密码!");
            $("#password-err").show();
            return;
        }
        if (passwd != passwd2) {
            $("#password2-err span").html("两次密码不一致!");
            $("#password2-err").show();
            return;
        }

        var data = {
            "mobile": mobile,
            "mobile_text": phoneCode,
            "pwd": passwd,
            "repwd": passwd2
        };

        //              转换为json字符串
        var data_json = JSON.stringify(data);

        $.ajax(
            {
                url:"/api/v1.0/user",
                data:data_json,
                type:"post",
                contentType:"application/json",
                dataType:"json", //返回数据类型
                success:function (data) {
                    alert(data.errno);
                    if(data.errno == 0){
                        //注册成功,跳转到首页
                        location.href = 'index.html';

                    }else {
                        //刷新当前页
                        location.reload(true);
                    }
                }
            }
        )




    });
})